package com.itheima.dubbo.mysql.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.commons.model.domain.BlackList;
import com.itheima.dubbo.interfaces.BlackListApi;
import com.itheima.dubbo.mysql.mapper.BlackListMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


@Service
public class BlackListApiImpl implements BlackListApi {

    @Autowired
    private BlackListMapper blackListMapper;

    @Override
    public void addBlacklist(BlackList blackList) {
        blackListMapper.insert(blackList);
    }

    /**
     * 查询黑名单列表
     *
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    public List<BlackList> queryBlacklist(Long userId, Integer page, Integer pageSize) {
        return blackListMapper.selectPage(new Page<>(page, pageSize),
                Wrappers.lambdaQuery(BlackList.class)
                        .eq(BlackList::getUserId, userId)
                        .orderByDesc(BlackList::getCreated)).getRecords();
    }

    /**
     * 删除黑名单
     *
     * @param userId
     * @param blackUserId
     */
    public void delBlacklist(Long userId, Long blackUserId) {
        this.blackListMapper.delete(Wrappers.lambdaQuery(BlackList.class)
                .eq(BlackList::getUserId, userId)
                .eq(BlackList::getBlackUserId, blackUserId));
    }
}
